# -*- coding: utf8-*-
_author_ = 'yykrlc'

# import logging
# from app_config import BaseConfig, basedir
# log = logging.getLogger('nds_coalition_video')
# log_file = '/'.join([basedir, "nds_coalition_video.log"])
# hdlr = logging.FileHandler(log_file)
# #formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
# formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
# hdlr.setFormatter(formatter)
# log.addHandler(hdlr)
# log.setLevel(logging.DEBUG)


import logging
from logging import handlers
from app_config import basedir


class Logger(object):
    level_relations = {
        'debug': logging.DEBUG,
        'info': logging.INFO,
        'warning': logging.WARNING,
        'error': logging.ERROR,
        'crit': logging.CRITICAL
    }  # 日志级别关系映射

    def __init__(self, filename, log_file_error, level='info',
                 fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'):
        self.logger = logging.getLogger(filename)
        format_str = logging.Formatter(fmt)  # 设置日志格式
        self.logger.setLevel(self.level_relations.get(level))  # 设置日志级别

        sh = logging.StreamHandler()  # 往屏幕上输出
        sh.setFormatter(format_str)  # 设置屏幕上显示的格式

        file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=1024 * 1024 * 10, backupCount=10)
        file_handler.setFormatter(format_str)  # 设置文件里写入的格式

        file_handler_error = logging.handlers.RotatingFileHandler(log_file_error, maxBytes=1024 * 1024 * 10,
                                                                  backupCount=10)
        file_handler_error.setFormatter(format_str)
        file_handler_error.setLevel('ERROR')

        self.logger.addHandler(sh)  # 把对象加到logger里
        self.logger.addHandler(file_handler)
        self.logger.addHandler(file_handler_error)


log_file = '/'.join([basedir, "nds_coalition_video.log"])
log_file_error = '/'.join([basedir, "nds_coalition_video_error.log"])
log_ger = Logger(log_file, log_file_error, level='info')
log = log_ger.logger
if __name__ == '__main__':
    log.debug('debug')
    log.info('info')
    log.warning('警告')
    log.error('报错')
    log.critical('严重')
# Logger(log_file_error, level='error').logger.error('error')